package com.java.task20.work1;

public class Workers {
    //创建两个成员方法  name  age
    private String name;
    private int age;

    //创建两个构造方法
    public Workers() {
    }

    public Workers(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //get和set方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        //判断name是否为null或者为""
        if (name != null && !name.equals("")) {
            this.name = name;
        } else {
            throw new NullPointerException("不能为空噢！");//如果为空则空指针异常(NullPointerException)
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        //判断age是否合理
        if (age >= 0 && age <= 150) {
            this.age = age;
        } else {
            throw new AgeException("年龄不可理！");//如果不合理则为年龄异常(AgeException)
        }
    }

    //重写的toString方法
    @Override
    public String toString() {
        return "Workers{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
